<!DOCTYPE html>
<html  lang="en">
<head>
    <meta charset="utf-8">
<title>noback</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />



    <meta property="og:type" content="website">
<meta property="og:title" content="noback">
<meta property="og:url" content="http://alpaca-h.gitee.io/page/2/index.html">
<meta property="og:site_name" content="noback">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://alpaca-h.gitee.io/images/og_image.png">
<meta property="article:author" content="Alpaca">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://alpaca-h.gitee.io/images/og_image.png">







<link rel="icon" href="/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/androidstudio.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    

    
    
    
        <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    

    


<link rel="stylesheet" href="/css/style.css">
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="noback" type="application/atom+xml">
</head>
<body class="is-3-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/">
            
                <img src="/images/logo.svg" alt="noback" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/">Home</a>
                
                <a class="navbar-item"
                href="/archives/">Archives</a>
                
                <a class="navbar-item"
                href="/categories/">Categories</a>
                
                <a class="navbar-item"
                href="/tags/">Tags</a>
                
                <a class="navbar-item"
                href="/about/">About</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                
                <a class="navbar-item search" title="Search" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-6-widescreen has-order-2 column-main">
    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/mysql/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%86%85%E5%AE%B9%E6%80%BB%E7%BB%93/"><i class="fas fa-angle-double-right"></i>数据库总结</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:52:59.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.071Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/mysql/">mysql</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    4 minutes read (About 604 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h2 id="登陆数据库"><a href="#登陆数据库" class="headerlink" title="登陆数据库"></a>登陆数据库</h2><pre><code class="bash">ssh 10.0.6.55
mysql -uroot -p
&gt;123456 </code></pre>
<h2 id="mysql-基本操作语句"><a href="#mysql-基本操作语句" class="headerlink" title="mysql 基本操作语句"></a>mysql 基本操作语句</h2><p>数据库语句操作</p>
<pre><code class="sql">创建一个名为 tc 的数据库 create database tc;
查看有哪些数据库 show databases; 
查看数据库的创建细节  show create database tc;         
创建一个使用gbk字符集的数据库 create database tc set gck;
修改字符集 alter database tc character set utf8;
删除创建的某个数据库 drop database tc;</code></pre>
<p>表结构</p>
<pre><code class="sql">use tc;  # 切换数据库
create table employee(_id int,name varchar(100),gender varchar(20),birthday date,entry_day date,salary flot(8,2),resume text);  # 创建员工信息表

show tables; # 查看当前数据库中的所有表
show create table employee; # 查看表的创建心结
alter table employee add image blob; # 在员工表上添加image列
alter table employee modify job varchar(60); # 修改员工表中的job列，使其长度我饿60
alter table employee drop image; 删除image列
rename table employee to user; 表名字修改为user
alter table user character set gbk; 修改表的字符集为utf-8</code></pre>
<p>查看表</p>
<pre><code class="sql"># 创建一个学生表
create table student(id int primary key auto_increment,name varchar(20) unique not null ,chinese float,english float,math float);

# 往学生表中添加一些信息
insert into student(name,chinese,english,math) values (&#39;wangba&#39;,93,22,77)
insert into student (name,chinese,english,math) values(&#39;lisi&#39;,80,90,29);
insert into student (name,chinese,english,math) values(&#39;wangwu&#39;,55,99,98);
insert into student (name,chinese,english,math) values(&#39;zhaoliu&#39;,99,30,57);
insert into student (name,chinese,english,math) values(&#39;zhouqi&#39;,78,22,77);

#查询所有学生信息
select * from user

# 查询学生员工的姓名和对应的英语成绩
select name,english from student

# 过滤表中的重复数据
select distinct english from student 

# 在所有学生分数上加10分的特长分
</code></pre>
<h2 id="数据库可视化-navicat"><a href="#数据库可视化-navicat" class="headerlink" title="数据库可视化 navicat"></a>数据库可视化 navicat</h2><h3 id="navicat连接远程服务器的几种方式"><a href="#navicat连接远程服务器的几种方式" class="headerlink" title="navicat连接远程服务器的几种方式"></a>navicat连接远程服务器的几种方式</h3><ol>
<li><p>直接连接数据库</p>
<pre><code class="bash">Connection Name : hzj #随便写  主要记录的上这条连接的别名
Host 10.0.6.55   # 远程服务器ip
Port 3306 # 端口
User Name root #  数据库账号
Passwd #数据库密码</code></pre>
</li>
<li><p>先登陆ssh 之后在连接本地的数据库</p>
</li>
</ol>
<p>先连接ssh</p>
<pre><code class="bash">Host # 远程
Port 22
User Name # 连接数据库的用户名
Authentication Method # 验证方法
Private key # id_rsa
Passphrase # 验证短密 选择密钥后自动生成</code></pre>
<p>再连接数据库</p>
<pre><code class="bash">Connection Name : hzj #随便写  主要记录的上这条连接的别名
Host localhost   # 远程服务器ip
Port 3306 # 端口
User Name root #  数据库账号
Passwd #数据库密码</code></pre>
<p>出现的错误</p>
<pre><code class="bash">2013 - Lost connection to MySQL server at &#39;reading initial communication packet&#39;, system error: 0 &quot;Internal error/check (Not system error)&quot; </code></pre>
<p>解决办法<br><a href="https://www.jb51.net/article/51480.htm" target="_blank" rel="noopener">https://www.jb51.net/article/51480.htm</a></p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/git/git%E5%9F%BA%E7%A1%80/"><i class="fas fa-angle-double-right"></i>git基础</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:50:50.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2020-01-02T10:33:48.993Z"><i class="far fa-calendar-check">&nbsp;</i>2020-01-02</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/git/">git</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    11 minutes read (About 1689 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="流程"><a href="#流程" class="headerlink" title="流程"></a>流程</h1><p>工作区(我们当前的文件夹)<br>暂存区(add 提交之后文件所存在的位置)<br>仓库(commit 之后将暂存区提交到的地方)</p>
<p>首先是init初始化我们的工作区，这时候当前存在.git文件的就是我们的工作区，当前路径为我们的根目录<br>在工作区创建一个新的文件test.txt，<br>git add 之后文件上传到暂存区，我们可以用git status 会记录当前暂存区的操作<br>git diff可以比较工作区和暂存区的差异，当然这里是没有任何差异的，因为他们是同样的两份文件<br>修改test.txt 之后再去git diff 你会发现他们之间存在着差异，因为这个时候他们已经不是同样两份文件了<br>假如你再次 使用Git add . 再用git diff 时又没有差异了</p>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><pre><code class="bash"># ubuntu
sudo apt-get install git
# centos 
yum install git -y
# mac 
brew install git</code></pre>
<h2 id="全局声明用户"><a href="#全局声明用户" class="headerlink" title="全局声明用户"></a>全局声明用户</h2><p>git config –global user.name “your name”<br>git config –global user.email “<a href="mailto:xxx@xxx.com">xxx@xxx.com</a>“</p>
<h2 id="声明仓库-必须"><a href="#声明仓库-必须" class="headerlink" title="声明仓库  必须"></a>声明仓库  必须</h2><p>mkdir test<br>cd test<br>git init </p>
<p>会生成一个.git的仓库管理文件,如果你使用了zsh的主题，你会发现多了一个master 这是你当前的分支，也仅仅只有这个分支</p>
<p>git add . —&gt; 将文件放到暂存区<br>git commit —&gt; 将文件提交到当前分支 master</p>
<p><strong>声明仓库内容  不一定</strong><br>既然是作为开源共享的项目或者说是线上多平台交互的私库，那一定需要一个专门解释这个仓库是做什么的文件—&gt; README.md<br>git add README.md</p>
<h2 id="提交并提供信息"><a href="#提交并提供信息" class="headerlink" title="提交并提供信息"></a>提交并提供信息</h2><p>将文件提交到仓库</p>
<pre><code class="bash">git commit -m &quot;your message&quot;</code></pre>
<h2 id="添加文件"><a href="#添加文件" class="headerlink" title="添加文件"></a>添加文件</h2><pre><code class="bash">git add xxx.txt
git add . #添加当前文件夹所有文件 </code></pre>
<h2 id="查看状态"><a href="#查看状态" class="headerlink" title="查看状态"></a>查看状态</h2><p>git status</p>
<pre><code class="bash"># 添加文件后的状态
 alpaca@hzj  ~/hzj/tu   master ●  git add README.md
 alpaca@hzj  ~/hzj/tu   master ✚  git status
On branch master
Changes to be committed:
  (use &quot;git reset HEAD &lt;file&gt;...&quot; to unstage)

        modified:   README.md


# 提交 commit 后的状态
 alpaca@hzj  ~/hzj/tu   master ✚  git commit -m &quot;update some file&quot;
[master ff070f9] update some file
 1 file changed, 2 insertions(+)
 alpaca@hzj  ~/hzj/tu   master 

# 状态
  alpaca@hzj  ~/hzj/tu   master  git status
On branch master
nothing to commit, working tree clean</code></pre>
<h2 id="查看日志"><a href="#查看日志" class="headerlink" title="查看日志"></a>查看日志</h2><p>当你每一次使用commit 将文件提交到当前分支的时候，记录下每一个步骤<br>查看使用</p>
<pre><code class="bash"># 查看日志
git log

commit bb3c0e3a1e850af0c70d3ce3a22995eaae177e82 (HEAD -&gt; master)
Author: alpaca &lt;1097690268@qq.com&gt;
Date:   Thu Nov 21 11:46:24 2019 +0800

    update

commit ff070f92dee18935752e5a98b2310d275e1191b6
Author: alpaca &lt;1097690268@qq.com&gt;
Date:   Thu Nov 21 11:42:58 2019 +0800

    update some file

commit 230660d585a95b90e3c6176ad21664c1b10f746a
Author: alpaca &lt;1097690268@qq.com&gt;
Date:   Thu Nov 21 10:37:04 2019 +0800

    update some markdown file


# 仅查看版本号
git log --pretty=oneline
# 指定版本号 如果你装了zsh 按tab就会出来commit_id
git log commit_id </code></pre>
<p>由随机码(版本号) + 作者 + 日期 + commit_message 组成</p>
<h2 id="回退操作"><a href="#回退操作" class="headerlink" title="回退操作"></a>回退操作</h2><p>版本回退，首先我们要知道一个事情 git log 只会记录下commit的过程，也就是上传到当前分支的过程，因此回退操作他并不会记录，但是他会重载之前的log状态，也就是当你一共提交了3次内容，你回退到了第三次提交前的版本。那么你的第三次提交log并不会被记录</p>
<pre><code class="bash"># 回退add操作  checkout -- file  暂存区 --&gt; 工作区
git checkout -- file

# 回退connit操作上  仓库 ---&gt; 暂存区
git reset --hard commit_id 

# 查看回退日志
git reflog</code></pre>
<h2 id="提交远程仓库"><a href="#提交远程仓库" class="headerlink" title="提交远程仓库"></a>提交远程仓库</h2><p>之前的操作都是本地的，包括最后一步git commit 也只是提交到了本地的仓库。为了更好的保存内容，我们已经引入云仓库或者说托管我们的项目</p>
<pre><code class="bash"># 添加远程仓库地址
git remote add origin https://gitee.com/Alpaca-H/how-to-learn-git.git 

# 提交当前分支到远程仓库
git push  origin master

# 建议第一次使用push -u
# Git不但会把本地的master分支内容推送的远程新的master分支，还会把本地的master分支和远程的master分支关联起来 在以后的推送或者拉取时就可以简化命令。</code></pre>
<h2 id="从远程仓库下载"><a href="#从远程仓库下载" class="headerlink" title="从远程仓库下载"></a>从远程仓库下载</h2><pre><code class="bash">git clone https://gitee.com/Alpaca-H/how-to-learn-git.git</code></pre>
<p>第一次提交的存在的问题</p>
<pre><code class="bash"> ! [rejected]        master -&gt; master (fetch first)
error: failed to push some refs to &#39;https://gitee.com/Alpaca-H/how-to-learn-git.git&#39;
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., &#39;git pull ...&#39;) before pushing again.
hint: See the &#39;Note about fast-forwards&#39; in &#39;git push --help&#39; for details. </code></pre>
<p>如果你是在 码云上创建的项目，它本身就存在两个README文件，也就是说仓库中本身就存在文件了，这时候你删掉这两个也没用，(github上好像没这个问题)<br>我们需要同步远程仓库，也就是拉取下来并用–allow-unrelated-histories 合并</p>
<pre><code class="bash">git pull origin master --allow-unrelated-histories   
git commit -m &quot;xxx&quot; #重新提交
git push origin master</code></pre>
<h2 id="从远程仓库下载-–-指定分支"><a href="#从远程仓库下载-–-指定分支" class="headerlink" title="从远程仓库下载 – 指定分支"></a>从远程仓库下载 – 指定分支</h2><pre><code class="bash">git clone -b hzj_branch https://gitee.com/Alpaca-H/how-to-learn-git.git </code></pre>
<h2 id="分支管理"><a href="#分支管理" class="headerlink" title="分支管理"></a>分支管理</h2><pre><code class="bash"># 创建分支  
git branch hzj
# 切换分支
git checkout hzj

# 创建并切换分支
git checkout -b hzj

# 查看当前分支
git branch</code></pre>
<p>记住上面的只是分支管理，他只是在你的本地有了分支，在你没有push之前所有的一切都是本地操作</p>
<h2 id="分支管理2"><a href="#分支管理2" class="headerlink" title="分支管理2"></a>分支管理2</h2><pre><code class="bash"># 切换到新的分支 
git swicth -c dev
# 切换到已有分支
git swicth dev</code></pre>
<pre><code class="bash"># 添加文件
echo &quot;我是hzj分支&quot; &gt; README2.md
git add README2.md
git commit -m &quot;update hzj&quot;
git push -u origin  hzj # 提交到hzj的分支</code></pre>
<h2 id="合并分支"><a href="#合并分支" class="headerlink" title="合并分支"></a>合并分支</h2><p>进入master</p>
<pre><code class="bash">git  merge hzj # 合并在本地仓库的文件</code></pre>
<h2 id="删除分支"><a href="#删除分支" class="headerlink" title="删除分支"></a>删除分支</h2><pre><code class="bash">git branch -d hzj </code></pre>
<h2 id="分支同步主干"><a href="#分支同步主干" class="headerlink" title="分支同步主干"></a>分支同步主干</h2><pre><code class="bash"># 切换到master分支
git checkout master 
# 下拉master
git pull origin master
# 切换分支
git checkout hzj
# 同步分支
git merge  master</code></pre>
<h2 id="主干同步分支-慎用"><a href="#主干同步分支-慎用" class="headerlink" title="主干同步分支(慎用)"></a>主干同步分支(慎用)</h2><pre><code class="bash"># 切换到主分支
git checkout master
# 同步分支
git merge hzj
# 提交
git push origin master </code></pre>
<h2 id="inventory操作"><a href="#inventory操作" class="headerlink" title="inventory操作"></a>inventory操作</h2><ol>
<li>拉取项目<br>git clone <a href="https://gitlab.s.upyun.com/infrastructure/inventory.gigit" target="_blank" rel="noopener">https://gitlab.s.upyun.com/infrastructure/inventory.gigit</a> clone <a href="https://gitlab.s.upyun.com/infrastructure/inventory.git" target="_blank" rel="noopener">https://gitlab.s.upyun.com/infrastructure/inventory.git</a></li>
<li>查看当前分支<br>git branch</li>
<li>切换分支<br>git checkout hzj</li>
<li>修改内容<br>…</li>
<li>上传<br>git commit -am “update”<br>git push<br>到web端创建合并请求</li>
<li>更新<br>首先切换到master，<br>拉取最新git pull<br>切换次分支 git checkout hzj<br>合并更新分支到hzj分支 git merge origin hzj</li>
</ol>
<p>发消息给有master权限的人，交由master来合并</p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/drf%E6%A1%86%E6%9E%B6/jwt/"><i class="fas fa-angle-double-right"></i>jwt</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:48:40.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.071Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    a few seconds read (About 85 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="使用jwt-json-web-token"><a href="#使用jwt-json-web-token" class="headerlink" title="使用jwt  json web token"></a>使用jwt  json web token</h1><pre><code class="bash">source /bin/active
pip install djangorestframework-jwt </code></pre>
<p><a href="https://zhuanlan.zhihu.com/p/30524397" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/30524397</a><br><a href="https://jpadilla.github.io/django-rest-framework-jwt/#security" target="_blank" rel="noopener">https://jpadilla.github.io/django-rest-framework-jwt/#security</a></p>
<h2 id="vue中使用token验证"><a href="#vue中使用token验证" class="headerlink" title="vue中使用token验证"></a>vue中使用token验证</h2><p><a href="https://blog.csdn.net/baiqiangdoudou/article/details/100174505" target="_blank" rel="noopener">https://blog.csdn.net/baiqiangdoudou/article/details/100174505</a></p>
<pre><code class="bash">curl -H &quot;Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTc0OTMwMjYyLCJlbWFpbCI6IjEwOTc2OTAyNjhAcXEuY29tIn0.cxzZhCOStN_w9OpVcq7rO-bcA_vWA172DtQaNdeGF3A&quot; http://localhost:8000/switch/ </code></pre>
<p><font color='red'>为什么要用token 与其他的差别</font></p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/drf%E6%A1%86%E6%9E%B6/drf-note/"><i class="fas fa-angle-double-right"></i>drf-note</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:48:11.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.071Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    a few seconds read (About 102 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h2 id="drf"><a href="#drf" class="headerlink" title="drf"></a>drf</h2><p>如何使用django-rest-framework</p>
<p>1.首先下载</p>
<pre><code class="python">pip3 install django-rest-framework</code></pre>
<p>2.应用<br>在setting中添加</p>
<pre><code class="python">INSTALLED_APPS = (
    ...
    &#39;rest_framework&#39;,
)</code></pre>
<p>3.在drf中使用登陆登出的功能</p>
<pre><code class="python">urlpatterns = [
    ...
    url(r&#39;^api-auth/&#39;, include(&#39;rest_framework.urls&#39;))
]</code></pre>
<hr>
<h2 id="序列化"><a href="#序列化" class="headerlink" title="序列化"></a>序列化</h2><p>创建model 与 django类似<br>创建serializer 序列化model数据，以json的形式展示  （serializer modelserialzier）<br>创建view  与django类似</p>
<p>使用httpie查看</p>
<p>pip install httpie<br>http <a href="http://localhost:8000/snippets/" target="_blank" rel="noopener">http://localhost:8000/snippets/</a></p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/drf%E6%A1%86%E6%9E%B6/drf%E6%A1%86%E6%9E%B6%E8%A7%A3%E6%9E%901/"><i class="fas fa-angle-double-right"></i>drf框架解析</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:47:38.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.071Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    2 minutes read (About 340 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="drf框架解析1"><a href="#drf框架解析1" class="headerlink" title="drf框架解析1"></a>drf框架解析1</h1><p>在drf的view视图中，我们经常会用到viewset视图集的内容。<br>引用的方法有很多种</p>
<ol>
<li>直接引用 我们可以使用ModelViewSet</li>
<li>根据不同的需求添加不同的mixins <pre><code class="bash"># 列出列表
class SwitchViewSet(viewsets.GenericViewSet,
                 mixins.ListModelMixin):
 serializer_class = SwitchSerializer
 queryset = SwitchModel.objects.all()</code></pre>
其中mixins有很多种</li>
</ol>
<ul>
<li>ListModelMixin</li>
<li>UpdateModelMixin</li>
<li>CreateModelMixin</li>
<li>RetrieveModelMixin</li>
<li>DestroyModelMixin<br>这些mixins本身就已经提供了最基础的功能，如果我们需要添加附加的需求，可以重写这些方法<h2 id="mixins中的内容"><a href="#mixins中的内容" class="headerlink" title="mixins中的内容"></a>mixins中的内容</h2></li>
</ul>
<pre><code class="python">class SwitchViewSet(viewsets.GenericViewSet,
                    mixins.ListModelMixin):
    # 获取序列化工具
    serializer_class = SwitchSerializer
    # 获取集合
    queryset = SwitchModel.objects.all()
</code></pre>
<h3 id="ListModelMixin"><a href="#ListModelMixin" class="headerlink" title="ListModelMixin"></a>ListModelMixin</h3><pre><code class="python">class ListModelMixin:
    &quot;&quot;&quot;
    List a queryset.
    &quot;&quot;&quot;
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
</code></pre>
<p>ListModelMixin 也就是get请求返回当前模型所有列表,filter_queryset达成过滤  get_queryset获取集合<br>第二段判断是否有做分类功能<br>第三段返回serializer</p>
<pre><code class="python"> def filter_queryset(self, queryset):
        &quot;&quot;&quot;
        Given a queryset, filter it with whichever filter backend is in use.

        You are unlikely to want to override this method, although you may need
        to call it either from a list view, or from a custom `get_object`
        method if you want to apply the configured filtering backend to the
        default queryset.
        &quot;&quot;&quot;
        for backend in list(self.filter_backends):
            queryset = backend().filter_queryset(self.request, queryset, self)
        return queryset </code></pre>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/django%E7%94%A8%E6%88%B7/"><i class="fas fa-angle-double-right"></i>django 用户</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:47:21.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.070Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    9 minutes read (About 1304 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="django用户"><a href="#django用户" class="headerlink" title="django用户"></a>django用户</h1><p>django为开发者提供了三种不同的用户形式，一种是自带的用户模型(User),另外两种是可以自定义的用户模型，分别是(AbstractUser)(AbstractBaseUser),他们的继承关系分别是 User 继承子AbstractUser 继承自AbstractBaseUser</p>
<h2 id="django自带的用户模型"><a href="#django自带的用户模型" class="headerlink" title="django自带的用户模型"></a>django自带的用户模型</h2><p>使用django自带的用户模型(快速实现)，该模型提供了一些开发过程中会经常使用到的参数，比如用户级别，名字，邮箱，密码等等。我们可以在$\color{red}{from django.contrib.auth.models import User}$中查看User的使用</p>
<pre><code class="python">class User(AbstractUser):
    &quot;&quot;&quot;
    Users within the Django authentication system are represented by this
    model.

    Username and password are required. Other fields are optional.
    &quot;&quot;&quot;
    class Meta(AbstractUser.Meta):
        swappable = &#39;AUTH_USER_MODEL&#39;</code></pre>
<h2 id="自定义User-Model"><a href="#自定义User-Model" class="headerlink" title="自定义User Model"></a>自定义User Model</h2><p>自定义Usermodel需要注意的是，$\color{red}{需要在setting.py中添加AUTH_USER_MODEL}$ ，由于django寻找用户的方式是在setting.py中寻找这个标示，所以在我们自定义的时候需要添加这个内容</p>
<pre><code class="python"># pro/setting.py
from app.model import .
AUTH_USER_MODEL=&quot;app.modelname&quot;</code></pre>
<p>其他需要注意的地方，请看文章底部注意栏</p>
<h3 id="方法一：扩展AbstractUser"><a href="#方法一：扩展AbstractUser" class="headerlink" title="方法一：扩展AbstractUser"></a>方法一：扩展AbstractUser</h3><p>如果你觉得django自带的用户不能满足你所需要的字段，需要额外添加字段的话，你可以使用AbstractUser</p>
<pre><code class="python"># pro/app/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models

class NewUser(AbstractUser):
    new_field = models.CharField(max_length=100)

# pro/setting.py
AUTH_USER_MODEL=&quot;app.modelname&quot;</code></pre>
<h2 id="方法二：扩展AbstractBaseUser"><a href="#方法二：扩展AbstractBaseUser" class="headerlink" title="方法二：扩展AbstractBaseUser"></a>方法二：扩展AbstractBaseUser</h2><p>AbstractBaseUser中只含有3个field: password, last_login和is_active. 如果你对django user model默认的first_name, last_name不满意, 或者只想保留默认的密码储存方式, 则可以选择这一方式</p>
<p>自定义形式如AbstarctUser一样</p>
<p>使用AbstractBaseUser时，虽然他提供了User最核心的实现，比如password，但是我们依旧需要添加一些必须定义的关键字段和方法</p>
<p>USERNAME_FIELD，用户名称字段，必须设置，且 uniqe=True</p>
<pre><code class="python">class UserProfile(AbstractBaseUser):
    author = models.CharField(max_length=40,unique=True)

    USERNAME_FIELD = &#39;author&#39;</code></pre>
<p>REQUIRED_FIELDS，当通过createsuperuser管理命令创建一个用户时，用于提示一个字段名称列表,不能包含USERNAME_FIELD以及password字段</p>
<pre><code class="python"># pro/models.py

class UserProfile(AbstractBaseUser)
    date_of_birth = models.DateField()
    height = models.FloatField()

    REQUIRED_FIELDS=[&#39;date_of_birth&#39;,&#39;height&#39;]</code></pre>
<p>is_active   必须定义。 一个布尔属性，标识用户是否是 “active” 的。AbstractBaseUser默认为 Ture</p>
<p>get_full_name()<br>必须定义。 long格式的用户标识。</p>
<p>get_short_name()<br>必须定义。 short格式的用户标识。</p>
<p>可用方法<br>get_username()<br>返回 USERNAME_FIELD 的值。</p>
<p>is_anonymous()<br>一直返回 False。用来区分 AnonymousUser。</p>
<p>is_authenticated()<br>一直返回 Ture。用来告诉用户已被认证。</p>
<p>set_password(raw_password)<br>设置密码。按照给定的原始字符串设置用户的密码，taking care of the password hashing。 不保存 AbstractBaseUser 对象。如果没有给定密码，密码就会被设置成不使用，同用set_unusable_password()。</p>
<p>check_password(raw_password)<br>检查密码是否正确。 给定的密码正确返回 True。</p>
<p>set_unusable_password()<br>设置user无密码。 不同于密码为空，如果使用 check_password()，则不会返回True。不保存AbstractBaseUser 对象。</p>
<p>has_usable_password()<br>如果设置了set_unusable_password()，返回False。</p>
<p>get_session_auth_hash()<br>返回密码字段的HMAC。 Used for Session invalidation on password change.</p>
<h2 id="⚠️注意"><a href="#⚠️注意" class="headerlink" title="⚠️注意"></a>⚠️注意</h2><p>1.在创建任何迁移或者第一次运行 manager.py migrate 前设置 AUTH_USER_MODEL。设置AUTH_USER_MODEL对你的数据库结构有很大的影响。它改变了一些会使用到的表格，并且会影响到一些外键和多对多关系的构造。在你有表格被创建后更改此设置是不被 makemigrations 支持的，并且会导致你需要手动修改数据库结构，从旧用户表中导出数据，可能重新应用一些迁移。<br>2.由于Django的可交换模型的动态依赖特性的局限，你必须确保 AUTH_USER_MODEL 引用的模型在所属app中第一个迁移文件中被创建（通常命名为 0001_initial），否则你会碰到错误(The easiest way to construct a compliant custom User model is to inherit fromAbstractBaseUser. AbstractBaseUser provides the core implementation of a Usermodel, including hashed passwords and tokenized password resets. You must then provide some key implementation details:)</p>
<h2 id="引用User"><a href="#引用User" class="headerlink" title="引用User"></a>引用User</h2><p>如果你选择了自定义用户类型，那么当你在view.py中尝试使用外键引用他的时候，你会出现错误，你应该使用$\color{red}{django.contrib.auth.get_user_model来引用模型}</p>
<pre><code class="python">def get_user_model():
    &quot;&quot;&quot;
    Return the User model that is active in this project.
    &quot;&quot;&quot;
    try:
        return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
    except ValueError:
        raise ImproperlyConfigured(&quot;AUTH_USER_MODEL must be of the form &#39;app_label.model_name&#39;&quot;)
    except LookupError:
        raise ImproperlyConfigured(
            &quot;AUTH_USER_MODEL refers to model &#39;%s&#39; that has not been installed&quot; % settings.AUTH_USER_MODEL
</code></pre>
<p>同样的，他会根据setting中的AUTH_USER_MODEL来识别当前用户</p>
<pre><code class="python">from django.contrib.auth import get_user_model

User = get_user_model</code></pre>
<p><strong>指定用户</strong></p>
<pre><code class="python"># pro/app/models.py

class UserProfile(models.Model):
    author = models.ForeignKey(setting.AUTH_USER_MODEL)</code></pre>
<p><a href="https://www.cnblogs.com/huchong/p/9804635.html#_label0" target="_blank" rel="noopener">https://www.cnblogs.com/huchong/p/9804635.html#_label0</a><br><a href="https://blog.csdn.net/qq_37049050/article/details/79211059" target="_blank" rel="noopener">https://blog.csdn.net/qq_37049050/article/details/79211059</a></p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/drf%E6%A1%86%E6%9E%B6/drf%E6%A1%86%E6%9E%B6/"><i class="fas fa-angle-double-right"></i>DRF框架</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:42:23.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.071Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    15 minutes read (About 2186 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h2 id="rust风格"><a href="#rust风格" class="headerlink" title="rust风格"></a>rust风格</h2><p>Representational State Transfer  表现层状态转化（ 资源定位及资源操作）<br>表象层面说就是通过get,post,put,delete方式来实现前后台通信的一种轻量级,跨平台,跨语言架构设计风格的web服务</p>
<p>http不仅仅是传输协议，更是一种应用协议。REST，即Representational State Transfer的缩写。意为是”表现层状态转化”。RESTful表示一种风格，理解REST前需要理解资源，何谓资源，广义的资源是指可以操作的所有对象。可能是一个系统资源，如txt、jgp、xml …，亦可以是诸如自己定义的虚拟集合的抽象，如books、usrs、times。RESTfutl代表一种简洁、方便、快捷、高效、透明的架构，这取决于你怎样组合。具有如下特点：</p>
<p>1、规范化接口访问方式。这些http操作方法包括GET/POST/PUT/DELETE/OPTIONS等，每个操作方法都代表一个相同意义的操作，它向所有人透明地表明操作方式。比如GET只能读取/拉数据，当然你也可以是添加数据，但建议不要这么做，不然这样就失去了REST的意义。<br>GET 读取<br>POST 添加<br>PUT 修改<br>DELETE 删除<br>2、资源标识唯一。通过URI表示一个资源名称，形式/resource/patch。如/users，表示用户的组合，或用户群。当然还可以继续标识某个具体的一个用户，/users/11，表示id为11的用户。当然，你也可以又用一组/usrgroup/11的URI代表操作用户组，不过不建议这么做，因为这样从字面上重复了/users/11资源表示的内容。一个资源URI总是包含第一条实现的方法：<br>GET /users/11<br>POST /users/11<br>PUT /users/11<br>DELETE /users/11<br>当然，仅有这些还不足以包括资源操作的所有需求，所以还可以包含请求参数，如GET /users?type=list&amp;page=1。<br>3、状态的转化。这就是REST的真实含义，指它允许资源URI具有不同的表现形式。同一个URI，根据不同请求方式，执行的动作不同；还可以根据请求的Header Accept的不同返回不同的结果，如text/html、text/css、text/xml等等。也可以理解为body不同。如查询快递一般，可以上次查询，物品还在仓库，而过一段时间已经在路上了。它表示的是一个互动过程。<br>4、所有信息都包含在当前请求中。请求的方式包含在 Request Header的Method中，还可以包含Accept、Accept-Encoding、Accept-Language，使用Authentication、Cookie等信息表明身份。同样，服务端通过发送Content-Length、Content-Type响应执行情况。最重要的是，需要返回Status Code通知执行状态，如200 201 400 404 500等http code。REST认为，所有信息都能通过请求一次性发送，而不必再采用方式保存状态，请求的信息本身已经说明了请求的意义。<br>5、无状态性。这是REST最重要的特性之一，这个状态指的是客户端与服务端无需为每次保存请求状态，客户端请求不必考虑当前状态，不必考虑上下文。具体上说，就是不必使用session等工具跟踪、保存请求的特殊性。比如，无论是谁，从哪里发送，几时发送，对同一个URI资源发送请求的结果都是一样的。据传，这样的设计是为当一台服务器宕机时，另一台服务器可以无差别地响应对方的请求。客户端请求只认URI，而不需理后台的设计。<br>实际上，在如今执行的RESTful设计当中，完全能执行这个要求的很少，最彻底的云服务，大部份为RESTful-like的风格。<br>6、可实现请求缓存。通过缓存减少请求次数，在HTTP响应里利用Cache-Control、Expires、Last-Modified三个头字段，可以让浏览器缓存资源一段时间。</p>
<h2 id="drf的视图集"><a href="#drf的视图集" class="headerlink" title="drf的视图集"></a>drf的视图集</h2><p>ViewSet类只是一种类型的基于类的视图，即不提供任何方法的处理程序，例如.get()或.post()，而是提供操作，如.list()和.create()。<br><a href="http://www.sinodocs.cn/api-guide/viewsets.html" target="_blank" rel="noopener">http://www.sinodocs.cn/api-guide/viewsets.html</a></p>
<h3 id="drf字段更新"><a href="#drf字段更新" class="headerlink" title="drf字段更新"></a>drf字段更新</h3><p>默认只有list和create 对应的方法就是get 和post<br>partial = True 局部更新</p>
<pre><code class="python">class TestViewSet(viewsets.ModelViewSet):
    serializer_class = TestSerializer
    queryset = test_Model.objects.all()

 @action(methods=[&#39;patch&#39;], detail=False)
    def update_field(self, request, *args, **kwargs):
        request.data[&quot;text&quot;] = &quot;更新dsdsd&quot;
        instance = test_Model.objects.get(id=request.data[&#39;id&#39;])
        # instance = self.get_object() 会报错
        # https://docs.djangoproject.com/en/2.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object
        # 默认使用id查找
        serializer = self.get_serializer(instance,data=request.data,partial=True)
        serializer.is_valid()
        serializer.save()
        return Response(serializer.data,status=status.HTTP_200_OK)

    @action(methods=[&#39;delete&#39;],detail=False)
    def delete_field(self,request,*args,**kwargs):
        instance = test_Model.objects.get(id=request.data[&#39;id&#39;])
        if instance:
            self.perform_destroy(instance)
            return Response({&quot;flag&quot;:&quot;删除成功&quot;},status=status.HTTP_200_OK)
        else:
            return  Response(status=status.HTTP_400_BAD_REQUEST)</code></pre>
<h3 id="基本的视图集"><a href="#基本的视图集" class="headerlink" title="基本的视图集"></a>基本的视图集</h3><p>使用viewsets.ModelViewSet + DefaultRouter<br>其中base-name表示基础url  lookup表示查询参数(只根据主键查找)<br>获取局部地址 <a href="http://localhost:8080/{base-name}/{lookup}" target="_blank" rel="noopener">http://localhost:8080/{base-name}/{lookup}</a></p>
<h2 id="drf路由集"><a href="#drf路由集" class="headerlink" title="drf路由集"></a>drf路由集</h2><p>为了更加便捷的开发项目,drf设计了路由集的内容，免去了路由配置中的麻烦，当然他也同时兼容django的路由设计<br><a href="https://www.django-rest-framework.org/api-guide/routers/" target="_blank" rel="noopener">https://www.django-rest-framework.org/api-guide/routers/</a></p>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><pre><code class="python">from rest_framework import routers

router = DefaultRouter(trailing_slash=False) # 是否以斜杠/结尾
# 声明注册路由
# 同path(&#39;switch/&#39;,views.per_switch.as_view()),
router.register(prefix=&#39;users&#39;, viewset=UserViewSet,basename=&quot;another_name&quot;)
router.register(&#39;accounts&#39;, AccountViewSet)

# 添加规则，django主动扫描urlpatterns变量获取路由
urlpatterns = router.urls </code></pre>
<p>以上 </p>
<ul>
<li>prefix定义前缀</li>
<li>viewset定义视图</li>
<li>basename定义前缀别名</li>
</ul>
<p>形成url格式(本地) —&gt;<br><a href="http://localhost:8080/user/" target="_blank" rel="noopener">http://localhost:8080/user/</a><br><a href="http://localhost:8000/accounts/" target="_blank" rel="noopener">http://localhost:8000/accounts/</a></p>
<h3 id="兼容使用"><a href="#兼容使用" class="headerlink" title="兼容使用"></a>兼容使用</h3><pre><code class="python">framework rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(&#39;test&#39;,viewset=TestViewSet,base_name=&quot;test&quot;)

urlpatterns = [
    path(&#39;switch/&#39;,views.switch_list.as_view()),
]

# 变量累加
urlpatterns += router.urls </code></pre>
<h3 id="路由集url"><a href="#路由集url" class="headerlink" title="路由集url"></a>路由集url</h3><p>django的路由集是真滴变态，当你使用带值参数去查找列表中的特定内容时候，他所表现的url是<br>^users/{pk}/set_password/$<br>注意 pk在中间 set_password是你方法的名字<br>如果你需要修改这些内容，可以在@action中指定<br>url_path = set_password<br>url_name = change_password</p>
<pre><code class="python">class SwitchViewSet(viewsets.GenericViewSet,
                    mixins.ListModelMixin):
    serializer_class = SwitchSerializer
    queryset = SwitchModel.objects.all()

    # get
    @action(detail=True,url_path=&quot;xx&quot;)
    def list_peer(self, request, pk):
        queryset = SwitchModel.objects.get(name=pk)
        print(queryset)
        # page = self.paginate_queryset(queryset)
        # if page is not None:
        #    serializer = self.get_serializer(page, many=True)
        #    return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset)
        return Response(serializer.data) </code></pre>
<p>如上一个列表查询的地址是  <a href="http://localhost:8080/{pk}/xx" target="_blank" rel="noopener">http://localhost:8080/{pk}/xx</a></p>
<h2 id="drf过滤"><a href="#drf过滤" class="headerlink" title="drf过滤"></a>drf过滤</h2><h3 id="drf通用过滤"><a href="#drf通用过滤" class="headerlink" title="drf通用过滤"></a>drf通用过滤</h3><pre><code class="bash"># setting.py
# 全局设置过滤器
REST_FRAMEWORK = {
    &#39;DEFAULT_FILTER_BACKENDS&#39;: [&#39;django_filters.rest_framework.DjangoFilterBackend&#39;]
}
# views.py
# 单视图设置过滤器
  filter_backends = [django_filters.rest_framework.DjangoFilterBackend]</code></pre>
<p><a href="https://www.django-rest-framework.org/api-guide/filtering/" target="_blank" rel="noopener">https://www.django-rest-framework.org/api-guide/filtering/</a></p>
<h2 id="drf分页"><a href="#drf分页" class="headerlink" title="drf分页"></a>drf分页</h2><p><a href="https://www.django-rest-framework.org/api-guide/pagination/" target="_blank" rel="noopener">https://www.django-rest-framework.org/api-guide/pagination/</a></p>
<h2 id="drf过滤器"><a href="#drf过滤器" class="headerlink" title="drf过滤器"></a>drf过滤器</h2><p><a href="https://www.django-rest-framework.org/api-guide/serializers/" target="_blank" rel="noopener">https://www.django-rest-framework.org/api-guide/serializers/</a></p>
<h2 id="drf解析器与渲染器"><a href="#drf解析器与渲染器" class="headerlink" title="drf解析器与渲染器"></a>drf解析器与渲染器</h2><p>视图的有效解析器集始终定义为类列表。当 request.data被访问时，REST框架将检查Content-Type传入请求上的标头，并确定使用哪个解析器来解析请求内容。<br>如果您未设置内容类型，则大多数客户端将默认使用’application/x-www-form-urlencoded’例如，如果要json使用jQuery和.ajax（）方法发送编码数据，则应确保包括该contentType: ‘application/json’设置。</p>
<h3 id="设置解析器"><a href="#设置解析器" class="headerlink" title="设置解析器"></a>设置解析器</h3><p>仅允许json的内容</p>
<pre><code class="bash"># setting.py
# 全局设置解析
REST_FRAMEWORK = {
    &#39;DEFAULT_PARSER_CLASSES&#39;: [
        &#39;rest_framework.parsers.JSONParser&#39;,
    ]
} 
# 单视图设置解析
class modelview():
    parser_classes = [JSONParser]</code></pre>
<p>其他各类解析<br><a href="https://www.django-rest-framework.org/api-guide/parsers/" target="_blank" rel="noopener">https://www.django-rest-framework.org/api-guide/parsers/</a></p>
<h2 id="渲染器"><a href="#渲染器" class="headerlink" title="渲染器"></a>渲染器</h2><p>既然有收的解析器，那也有发出数据的渲染器，<br>REST框架包括许多内置的Renderer类，使您可以返回各种媒体类型的响应。还支持定义自己的自定义渲染器，这使您可以灵活地设计自己的媒体类型。</p>
<h3 id="设置渲染器"><a href="#设置渲染器" class="headerlink" title="设置渲染器"></a>设置渲染器</h3><pre><code class="bash"># setting.py
# 全局设置解析
REST_FRAMEWORK = {
    &#39;DEFAULT_RENDERER_CLASSES&#39;: [
        &#39;rest_framework.renderers.JSONRenderer&#39;,
        &#39;rest_framework.renderers.BrowsableAPIRenderer&#39;,
    ]
} 
# 单视图设置解析
class modelview()
    renderer_classes = [JSONRenderer]</code></pre>
<p>其他渲染器<br><a href="https://www.django-rest-framework.org/api-guide/renderers/" target="_blank" rel="noopener">https://www.django-rest-framework.org/api-guide/renderers/</a></p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/django%E5%AD%97%E6%AE%B5%E6%80%BB%E7%BB%93/"><i class="fas fa-angle-double-right"></i>django字段总结</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:41:01.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.070Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    5 minutes read (About 763 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="django字段的设计"><a href="#django字段的设计" class="headerlink" title="django字段的设计"></a>django字段的设计</h1><p><a href="https://docs.djangoproject.com/zh-hans/2.2/ref/models/fields/#module-django.db.models.fields" target="_blank" rel="noopener">https://docs.djangoproject.com/zh-hans/2.2/ref/models/fields/#module-django.db.models.fields</a><br>orm的设计模式<br>在django的应用过程中，往往要设计model的设计，在model设计过程中要使用各种各样的关键词，这些关键词 关联到了数据库中每条记录的配置</p>
<pre><code class="python"># 引用
from django.db import models </code></pre>
<h2 id="通用字段"><a href="#通用字段" class="headerlink" title="通用字段"></a>通用字段</h2><h3 id="null字段"><a href="#null字段" class="headerlink" title="null字段"></a>null字段</h3><p>如果设置为 True， 当该字段为空时，Django 会将数据库中该字段设置为NULL，默认为 False。<br>即数据库置空 如果null=true blank=true 则数据库内容为空白字符串</p>
<h3 id="blank"><a href="#blank" class="headerlink" title="blank"></a>blank</h3><p>Field.blank<br>默认blank = False ,如果设置为True，则允许该字段为空。<br>与null不同的是,null存粹与数据库有相关，而blank与验证相关。如果blank=true,则表单验证将允许输入空值,如果blank=False 则不允许输入空值</p>
<hr>
<h3 id="choices"><a href="#choices" class="headerlink" title="choices"></a>choices</h3><pre><code class="python">class TaskSwitchModel(models.Model):
    ActionType = models.CharField(choices=[
        (&#39;snmp&#39;,&#39;_(snmp)&#39;),
        (&quot;vlan&quot;,&quot;_vlan&quot;),
        (&quot;sysname&quot;,&quot;_sysname&quot;),
        (&quot;copyright-info&quot;,&quot;_copyright-info&quot;),
        (&quot;int-vlan&quot;,&quot;_int-vlan&quot;),
        (&quot;int&quot;,&quot;_int&quot;)],max_length=100,default=&quot;&quot;)
    test = models.CharField(max_length=1, choices=[(&#39;A&#39;, (&#39;Author&#39;)), (&#39;E&#39;, (&#39;Editor&#39;))]), </code></pre>
<pre><code class="python">
class TaskSwitchModel(model.ModelForm):
    choices=(
        (&#39;snmp&#39;,&#39;_(snmp)&#39;),
        (&quot;vlan&quot;,&quot;_vlan&quot;),
        (&quot;sysname&quot;,&quot;_sysname&quot;),
        (&quot;copyright-info&quot;,&quot;_copyright-info&quot;),
        (&quot;int-vlan&quot;,&quot;_int-vlan&quot;),
        (&quot;int&quot;,&quot;_int&quot;)
    )
    ActionType = model.CharField(choices=choices,max_length=100,default=&quot;&quot;) </code></pre>
<p>Field.choices<br>每个元组中的第一个元素是要在模型上设置的实际值，第二个元素是人类可读的名称.</p>
<pre><code class="python">select_choices =[
    (&#39;A&#39;,&#39;A_&#39;),
    (&#39;B&#39;,&#39;B_&#39;),
    (&#39;C&#39;,&#39;C_&#39;),
    (&#39;D&#39;,&#39;D_&#39;),
]
choices字段迭代
每个元组的第一个元素是要应用于组的名称。第二个元素是一个可迭代的2代元组
select2_select1_choices=[
    (&#39;A&#39;,(
        (&#39;A_/&#39;,&#39;A_movie&#39;),
        (&#39;A_+&#39;,&#39;A_video&#39;),
        (&#39;A_-&#39;,&#39;A_City&#39;),
        )
    ),
    (&#39;Video&#39;, (
            (&#39;vhs&#39;, &#39;VHS Tape&#39;),
            (&#39;dvd&#39;, &#39;DVD&#39;),
        )
    ),
    (&#39;unknown&#39;, &#39;Unknown&#39;),
] </code></pre>
<h3 id="db-column"><a href="#db-column" class="headerlink" title="db_column"></a>db_column</h3><p>设置数据库列的名称    age = models.CharFiled(db_column=”年龄”)</p>
<h3 id="db-index"><a href="#db-index" class="headerlink" title="db_index"></a>db_index</h3><p>age = models.CharFiled(db_index=true)<br>如果设置为True，则创建数据库索引</p>
<h3 id="default"><a href="#default" class="headerlink" title="default"></a>default</h3><p>可以是值也可以是可调用的对象，每次实例化模型的时候 会调用这个对象或值</p>
<pre><code class="python"># 1
def contact_defult():
    return {&quot;email&quot;:&quot;1097690268@qq.com&quot;}

contact_info = JSONField(&quot;ContactInfo&quot;, default=contact_default)

# 2
member_name = CharFiled(default=&quot;小明&quot;)</code></pre>
<h3 id="editable"><a href="#editable" class="headerlink" title="editable"></a>editable</h3><p>可编辑字段<br>如果为False，则该字段将不会显示在管理员或其他任何人中 ModelForm。在模型验证期间也将跳过它们。默认值为True。</p>
<hr>
<h3 id="unique"><a href="#unique" class="headerlink" title="unique"></a>unique</h3><p>name = CharFiled(unique=true)<br>如上则会保证该字段的值为表中唯一，也就是不会出现同名的人<br>如果存在了unique 则不再需要db_index 因为unique默认存在索引</p>
<h2 id="primary-key"><a href="#primary-key" class="headerlink" title="primary_key"></a>primary_key</h2><p>如果设置为True，则初始设置为该模型的主键。<br>如果您未primary_key=True在模型中指定任何字段，则Django会自动添加一个AutoField来保留主键，因此primary_key=True暗示null=False和 unique=True。一个对象只允许使用一个主键。<br>主键字段是只读的。如果更改现有对象上的主键的值然后保存，则将在旧对象的旁边创建一个新对象。</p>
<h2 id="自定义的字段"><a href="#自定义的字段" class="headerlink" title="自定义的字段"></a>自定义的字段</h2><p><a href="https://docs.djangoproject.com/zh-hans/2.2/howto/custom-model-fields/" target="_blank" rel="noopener">https://docs.djangoproject.com/zh-hans/2.2/howto/custom-model-fields/</a></p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/django%E5%86%85%E5%AE%B9%E6%80%BB%E7%BB%93/"><i class="fas fa-angle-double-right"></i>django内容总结</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:40:44.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.070Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    13 minutes read (About 1914 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="django内容总结"><a href="#django内容总结" class="headerlink" title="django内容总结"></a>django内容总结</h1><h2 id="在后台对某些固定字段设置颜色"><a href="#在后台对某些固定字段设置颜色" class="headerlink" title="在后台对某些固定字段设置颜色"></a>在后台对某些固定字段设置颜色</h2><pre><code class="py"># models.py
from django.urls.html import format_html
class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)


    def colored_name(self):

        return format_html(

            &#39;&lt;span style=&quot;color: #{};&quot;&gt;{} {}&lt;/span&gt;&#39;,

            self.color_code,

            self.first_name,

            self.last_name,

        )</code></pre>
<h3 id="admin-py"><a href="#admin-py" class="headerlink" title="admin.py"></a>admin.py</h3><p>class PersonAdmin(admin.ModelAdmin):<br>    list_display = (‘first_name’, ‘last_name’, ‘colored_name’)</p>
<h2 id="DjangoAdmin"><a href="#DjangoAdmin" class="headerlink" title="DjangoAdmin"></a>DjangoAdmin</h2><blockquote>
<p>Django自带后台管理模板是其一大特色，但作为后台管理很多地方需要我们去改进，当然也可以使用一些其他的后台模板</p>
</blockquote>
<ul>
<li>推荐模板1: xadmin xadmin在几年前是为django定制的后台管理模板，但是作者已经停止维护，但是在其分支中依旧有部分人在维护。<br>  具体使用跳转到我的掘金<a href="https://juejin.im/post/5afd267d51882542714ff756" target="_blank" rel="noopener">https://juejin.im/post/5afd267d51882542714ff756</a></li>
<li>推荐模板2： django simple-ui 在github上的星数并不高，但是是基于饿了么element-ui所写的django后台模板，我还未使用</li>
<li>使用本身自带的admin模板</li>
<li>使用其他兼容多语言的后台模板</li>
</ul>
<p>url :<a href="https://my.oschina.net/zhiyonghe/blog/1532030" target="_blank" rel="noopener">https://my.oschina.net/zhiyonghe/blog/1532030</a></p>
<hr>
<h3 id="自带的Admin模板"><a href="#自带的Admin模板" class="headerlink" title="自带的Admin模板"></a>自带的Admin模板</h3><blockquote>
<p>这里介绍一下自带的admin模板。</p>
</blockquote>
<ol>
<li>admin界面汉化<br>在setting.py文件中设置语言<pre><code class="python"># setting.py
</code></pre>
</li>
</ol>
<p>LANGUAGE_CODE = ‘zh-hans’<br>IME_ZONE = ‘Asia/Shanghai’</p>
<pre><code>
2. Model注册
在Model.py中创建完类后，需要在admin.py中完成注册，才能在后台模板中显示，方法有二

```python
# admin.py
# 方法一
from django.contrib import admin 
from blog.models import Blog

class BlogAdmin(admin.ModelAdmin):
    list_display=(&#39;xx&#39;,&#39;xx&#39;,&#39;xx&#39;)  # 要显示的字段
    list_per_page = 50  # 每页最多显示50条
    ordering = (&quot;-xxx&quot;)  # 设置排序，-表示降序排序
    list_editable = [&#39;&#39;,&#39;&#39;,&#39;&#39;] # 设置可编辑字段
    list_display_links = (&#39;&#39;,&#39;&#39;,) # 默认情况下只能点击第一个字段进入编辑，设置多个字段可点击进入编辑
    list_filter = (&#39;&#39;,&#39;&#39;,&#39;&#39;) # 筛选器
    search_fields  = (&#39;&#39;,&#39;&#39;,&#39;&#39;,) # 搜索字段
    date_hierarchy = &#39;go_time&#39; # 详细时间筛选

admin.site.register(Blog,BlogAdmin)

# 方法二
from djang.contrib import admin
from blog.models import Blog

@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
    list_display=(&#39;&#39;,&#39;&#39;,&#39;&#39;)</code></pre><hr>
<h2 id="Django-for-ajax"><a href="#Django-for-ajax" class="headerlink" title="Django for ajax"></a>Django for ajax</h2><p>django 前后台传递消息内容的方法有很多种,这里我们来讲一下ajax for django</p>
<pre><code class="python"># 路由地址
# 项目路径下
path(&#39;&#39;,include(&#39;GraphPro.urls&#39;))
# 应用路径下
path(&#39;echartsdemo/&#39;,demoView.as_view(),name=&quot;demo&quot;),
# 视图层
class  demoView(View):
    def get(self,request):
        if request.GET.get(&#39;name&#39;) == &#39;hzj&#39;:
            return HttpResponse(&quot;ssdsdsd&quot;)
        else:
            print(2)
        return render(request,&#39;demo.html&#39;)
    def post(self,request):
        pass
# 模板层
&lt;body&gt;
    &lt;!-- 为ECharts准备一个具备大小（宽高）的Dom --&gt;
    &lt;div id=&quot;main&quot; style=&quot;width: 1000px;height:1000px;&quot; &gt;&lt;/div&gt;

    &lt;input type=&quot;text&quot; id=&quot;name&quot; value=&quot;hzj&quot;&gt;
    &lt;span id=&quot;result&quot;&gt;&lt;/span&gt;
    &lt;button class=&quot;button&quot; id=&quot;click_func&quot;&gt;点击&lt;/button&gt;
    {{ data }}


    &lt;script type=&quot;text/javascript&quot;&gt;

         $(document).ready(function () {
            var name = $(&#39;#name&#39;).val();
            var data = {&quot;name&quot;:name};
            $(&quot;#click_func&quot;).click(function () {
                $.get(
                    //请求的url
                    &#39;{% url 'demoecharts' %}&#39;,
                    data,
                    function (ret) {
                        console.log(ret)
                    }
                )
            })
        })


    &lt;/script&gt;
&lt;/body&gt;</code></pre>
<p>进入页面直接请求后台数据</p>
<pre><code class="html">
    &lt;script type=&quot;text/javascript&quot;&gt;

         $(document).ready(function () {
             var data = {&quot;name&quot;:&quot;hzj&quot;};
             $.get(
                //请求的url
                &#39;{% url 'demo' %}&#39;,
                data,
                function (ret) {
                    console.log(ret)
                }
             )
        })


    &lt;/script&gt;</code></pre>
<p>异步加载数据</p>
<pre><code class="html"> &lt;script type=&quot;text/javascript&quot;&gt;

         $(document).ready(function () {
             var data = {&quot;name&quot;:&quot;hzj&quot;};
             $.get(
                //请求的url
                &#39;{% url 'demo' %}&#39;,
                data,
             ).done(function(ret){
                 console.log(ret)
             })
        })


    &lt;/script&gt;
</code></pre>
<hr>
<blockquote>
<p>介绍Mysql在Django中的引用<br>由于Django在初始化项目后，所使用的数据库是sqlite3,但为了扩展，我们这里计划使用Mysql,配置步骤如下</p>
</blockquote>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><pre><code class="python">1.在服务器上安装Mysql数据库(Centos版)

2.在django的setting.py中引用</code></pre>
<hr>
<h2 id="错误类型总结"><a href="#错误类型总结" class="headerlink" title="错误类型总结"></a>错误类型总结</h2><p>由于mysql与django在配置过程中会存在很多的错误，这里总结一下</p>
<h4 id="Mysql在Centos中权限不足的错误"><a href="#Mysql在Centos中权限不足的错误" class="headerlink" title="Mysql在Centos中权限不足的错误"></a>Mysql在Centos中权限不足的错误</h4><pre><code class="text">ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)</code></pre>
<p>原因是/var/lib/mysql的访问权限不足</p>
<pre><code class="text">1.改变权限 chown -R mysql:mysql /var/lib/mysql   
2. 启动服务器 /etc/init.d/mysql start (会自动生成mysql.sock文件)  
3.重新启动mysql服务  /etc/init.d/mysql start</code></pre>
<h4 id="密码类型错误"><a href="#密码类型错误" class="headerlink" title="密码类型错误"></a>密码类型错误</h4><pre><code class="text">1045, &quot;Access denied for user &#39;root&#39;@&#39;122.224.83.xxx  </code></pre>
<p>原因 密码错误，注意密码不能是字符串</p>
<pre><code class="text">GRANT ALL PRIVILEGES ON *.* TO root@&#39;%&#39; IDENTIFIED BY &#39;123456&#39; WITH GRANT OPTION;</code></pre>
<h4 id="django-setting-中的配置"><a href="#django-setting-中的配置" class="headerlink" title="django setting 中的配置"></a>django setting 中的配置</h4><pre><code class="text">DATABASES = {
    &#39;default&#39;: {

        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,
        &#39;NAME&#39;: &#39;itemids&#39;,  # 数据库名字
        &#39;USER&#39;: &#39;root&#39;,    # 数据库登录名
        &#39;PASSWORD&#39;: &#39;12345x&#39;, # 数据库密码
        &#39;HOST&#39;: &#39;106.14.195.xxx&#39;, 数据库IP
        &#39;POST&#39;: 3306, # 端口
    }
}</code></pre>
<h4 id="引用pymysql"><a href="#引用pymysql" class="headerlink" title="引用pymysql"></a>引用pymysql</h4><p>由于MySQLdb只能使用在Python2中，在python3中已经停止了维护，所以这里我们引用pymyql的库</p>
<pre><code class="python">pipenv install pymysql
# 在与setting.py同级别的__init__.py中使用
import pymysql
pymysql.install_as_MySQLdb()</code></pre>
<h4 id="pymysql版本不符合"><a href="#pymysql版本不符合" class="headerlink" title="pymysql版本不符合"></a>pymysql版本不符合</h4><p>报错内容:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11<br>修改Pipenv\Lib\site-packages\django\db\backends\mysql\base.py<br>注释下面内容<br>if version &lt; (1, 3, 3):<br>    raise ImproperlyConfigured(“mysqlclient 1.3.3 or newer is required; you have %s” % Database.<strong>version</strong>)</p>
<p>(在Centos中，pipenv install 所生成的包 会在pipenv shell 激活环境的时候出现)</p>
<p>由于上面注释，还会出现的报错内容为<br>File “C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query<br>将operations.py中的decode改成encode</p>
<h4 id="mysql不能插入中文"><a href="#mysql不能插入中文" class="headerlink" title="mysql不能插入中文"></a>mysql不能插入中文</h4><p>报错内容： django.db.utils.InternalError: (1366, “Incorrect string value: ‘\xE5\xAE\x9A\xE6\x97\xB6…’ for column ‘name’ at row 1”)<br>解决方法: <a href="https://blog.csdn.net/tzh476/article/details/52644271" target="_blank" rel="noopener">https://blog.csdn.net/tzh476/article/details/52644271</a><br>删除之前的库，创建一个新的数据库，使用utf8mb64   之前默认创建的都是latin1</p>
<h4 id="mysql-长度的问题"><a href="#mysql-长度的问题" class="headerlink" title="mysql 长度的问题"></a>mysql 长度的问题</h4><p>django.db.utils.InternalError: (1071, ‘Specified key was too long; max key length is 767 bytes’)<br><a href="https://blog.csdn.net/ljfphp/article/details/80406907" target="_blank" rel="noopener">https://blog.csdn.net/ljfphp/article/details/80406907</a><br><a href="https://www.orcode.com/question/407126_k280b8.html" target="_blank" rel="noopener">https://www.orcode.com/question/407126_k280b8.html</a></p>
<p>解决方法1: 升级Mysql5.6–&gt;&gt;Mysql5.7<br>centos7 下  对mysql的操作 <a href="https://blog.csdn.net/xufengzhu/article/details/81110982" target="_blank" rel="noopener">https://blog.csdn.net/xufengzhu/article/details/81110982</a><br><a href="https://blog.51cto.com/lisea/1941616" target="_blank" rel="noopener">https://blog.51cto.com/lisea/1941616</a></p>
<p>解决方法2 : 先使用utf8格式</p>
<p>—-&gt; 介绍Mysql在Django中的引用<br>由于Django在初始化项目后，所使用的数据库是sqlite3,但为了扩展，我们这里计划使用Mysql,配置步骤如下</p>
<h2 id="配置-1"><a href="#配置-1" class="headerlink" title="配置"></a>配置</h2><pre><code class="python">1.在服务器上安装Mysql数据库(Centos版)

2.在django的setting.py中引用</code></pre>
<hr>
<h2 id="错误类型总结-1"><a href="#错误类型总结-1" class="headerlink" title="错误类型总结"></a>错误类型总结</h2><p>由于mysql与django在配置过程中会存在很多的错误，这里总结一下</p>
<h4 id="Mysql在Centos中权限不足的错误-1"><a href="#Mysql在Centos中权限不足的错误-1" class="headerlink" title="Mysql在Centos中权限不足的错误"></a>Mysql在Centos中权限不足的错误</h4><pre><code class="text">ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)</code></pre>
<p>原因是/var/lib/mysql的访问权限不足</p>
<pre><code class="text">1.改变权限 chown -R mysql:mysql /var/lib/mysql   
2. 启动服务器 /etc/init.d/mysql start (会自动生成mysql.sock文件)  
3.重新启动mysql服务  /etc/init.d/mysql start</code></pre>
<h4 id="密码类型错误-1"><a href="#密码类型错误-1" class="headerlink" title="密码类型错误"></a>密码类型错误</h4><pre><code class="text">1045, &quot;Access denied for user &#39;root&#39;@&#39;122.224.83.xxx  </code></pre>
<p>原因 密码错误，注意密码不能是字符串</p>
<pre><code class="text">GRANT ALL PRIVILEGES ON *.* TO root@&#39;%&#39; IDENTIFIED BY &#39;123456&#39; WITH GRANT OPTION;</code></pre>
<h4 id="django-setting-中的配置-1"><a href="#django-setting-中的配置-1" class="headerlink" title="django setting 中的配置"></a>django setting 中的配置</h4><pre><code class="text">DATABASES = {
    &#39;default&#39;: {

        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,
        &#39;NAME&#39;: &#39;itemids&#39;,  # 数据库名字
        &#39;USER&#39;: &#39;root&#39;,    # 数据库登录名
        &#39;PASSWORD&#39;: &#39;12345x&#39;, # 数据库密码
        &#39;HOST&#39;: &#39;106.14.195.xxx&#39;, 数据库IP
        &#39;POST&#39;: 3306, # 端口
    }
}</code></pre>
<h4 id="引用pymysql-1"><a href="#引用pymysql-1" class="headerlink" title="引用pymysql"></a>引用pymysql</h4><p>由于MySQLdb只能使用在Python2中，在python3中已经停止了维护，所以这里我们引用pymyql的库</p>
<pre><code class="python">pipenv install pymysql
# 在与setting.py同级别的__init__.py中使用
import pymysql
pymysql.install_as_MySQLdb()</code></pre>
<h4 id="pymysql版本不符合-1"><a href="#pymysql版本不符合-1" class="headerlink" title="pymysql版本不符合"></a>pymysql版本不符合</h4><p>报错内容:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11<br>修改Pipenv\Lib\site-packages\django\db\backends\mysql\base.py<br>注释下面内容<br>if version &lt; (1, 3, 3):<br>    raise ImproperlyConfigured(“mysqlclient 1.3.3 or newer is required; you have %s” % Database.<strong>version</strong>)</p>
<p>(在Centos中，pipenv install 所生成的包 会在pipenv shell 激活环境的时候出现)</p>
<p>由于上面注释，还会出现的报错内容为<br>File “C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query<br>将operations.py中的decode改成encode</p>
<h4 id="mysql不能插入中文-1"><a href="#mysql不能插入中文-1" class="headerlink" title="mysql不能插入中文"></a>mysql不能插入中文</h4><p>报错内容： django.db.utils.InternalError: (1366, “Incorrect string value: ‘\xE5\xAE\x9A\xE6\x97\xB6…’ for column ‘name’ at row 1”)<br>解决方法: <a href="https://blog.csdn.net/tzh476/article/details/52644271" target="_blank" rel="noopener">https://blog.csdn.net/tzh476/article/details/52644271</a><br>删除之前的库，创建一个新的数据库，使用utf8mb64   之前默认创建的都是latin1</p>
<h4 id="mysql-长度的问题-1"><a href="#mysql-长度的问题-1" class="headerlink" title="mysql 长度的问题"></a>mysql 长度的问题</h4><p>django.db.utils.InternalError: (1071, ‘Specified key was too long; max key length is 767 bytes’)<br><a href="https://blog.csdn.net/ljfphp/article/details/80406907" target="_blank" rel="noopener">https://blog.csdn.net/ljfphp/article/details/80406907</a><br><a href="https://www.orcode.com/question/407126_k280b8.html" target="_blank" rel="noopener">https://www.orcode.com/question/407126_k280b8.html</a></p>
<p>解决方法1: 升级Mysql5.6–&gt;&gt;Mysql5.7<br>centos7 下  对mysql的操作 <a href="https://blog.csdn.net/xufengzhu/article/details/81110982" target="_blank" rel="noopener">https://blog.csdn.net/xufengzhu/article/details/81110982</a><br><a href="https://blog.51cto.com/lisea/1941616" target="_blank" rel="noopener">https://blog.51cto.com/lisea/1941616</a></p>
<p>解决方法2 : 先使用utf8格式</p>

        </div>
        
        
        
        
    </div>
</div>









    <div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <a class="has-link-black-ter" href="/2019/12/29/blog_back_new/django/django%E9%94%99%E8%AF%AF%E5%90%88%E9%9B%86/"><i class="fas fa-angle-double-right"></i>django错误合集</a>
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-29T12:40:29.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-29</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-29T12:56:13.070Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-29</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/django/">django</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    4 minutes read (About 648 words)
                </span>
                
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="错误类型总结"><a href="#错误类型总结" class="headerlink" title="错误类型总结"></a>错误类型总结</h1><p>由于mysql与django在配置过程中会存在很多的错误，这里总结一下</p>
<h2 id="Mysql在Centos中权限不足的错误"><a href="#Mysql在Centos中权限不足的错误" class="headerlink" title="Mysql在Centos中权限不足的错误"></a>Mysql在Centos中权限不足的错误</h2><pre><code class="text">ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)</code></pre>
<p>原因是/var/lib/mysql的访问权限不足</p>
<pre><code class="text">1.改变权限 chown -R mysql:mysql /var/lib/mysql   
2. 启动服务器 /etc/init.d/mysql start (会自动生成mysql.sock文件)  
3.重新启动mysql服务  /etc/init.d/mysql start</code></pre>
<h2 id="密码类型错误"><a href="#密码类型错误" class="headerlink" title="密码类型错误"></a>密码类型错误</h2><pre><code class="text">1045, &quot;Access denied for user &#39;root&#39;@&#39;122.224.83.xxx  </code></pre>
<p>原因 密码错误，注意密码不能是字符串</p>
<pre><code class="text">GRANT ALL PRIVILEGES ON *.* TO root@&#39;%&#39; IDENTIFIED BY &#39;123456&#39; WITH GRANT OPTION;</code></pre>
<h2 id="django-setting-中的配置"><a href="#django-setting-中的配置" class="headerlink" title="django setting 中的配置"></a>django setting 中的配置</h2><pre><code class="text">DATABASES = {
    &#39;default&#39;: {

        &#39;ENGINE&#39;: &#39;django.db.backends.mysql&#39;,
        &#39;NAME&#39;: &#39;itemids&#39;,  # 数据库名字
        &#39;USER&#39;: &#39;root&#39;,    # 数据库登录名
        &#39;PASSWORD&#39;: &#39;12345x&#39;, # 数据库密码
        &#39;HOST&#39;: &#39;106.14.195.xxx&#39;, 数据库IP
        &#39;POST&#39;: 3306, # 端口
    }
}</code></pre>
<h2 id="引用pymysql"><a href="#引用pymysql" class="headerlink" title="引用pymysql"></a>引用pymysql</h2><p>由于MySQLdb只能使用在Python2中，在python3中已经停止了维护，所以这里我们引用pymyql的库</p>
<pre><code class="python">pipenv install pymysql
# 在与setting.py同级别的__init__.py中使用
import pymysql
pymysql.install_as_MySQLdb()</code></pre>
<h2 id="pymysql版本不符合"><a href="#pymysql版本不符合" class="headerlink" title="pymysql版本不符合"></a>pymysql版本不符合</h2><p>报错内容:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11<br>修改Pipenv\Lib\site-packages\django\db\backends\mysql\base.py<br>注释下面内容<br>if version &lt; (1, 3, 3):<br>    raise ImproperlyConfigured(“mysqlclient 1.3.3 or newer is required; you have %s” % Database.<strong>version</strong>)</p>
<p>(在Centos中，pipenv install 所生成的包 会在pipenv shell 激活环境的时候出现)</p>
<p>由于上面注释，还会出现的报错内容为<br>File “C:\Users\Administrator\PycharmProjects\untitled1\venv\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query<br>将operations.py中的decode改成encode</p>
<h2 id="mysql不能插入中文"><a href="#mysql不能插入中文" class="headerlink" title="mysql不能插入中文"></a>mysql不能插入中文</h2><p>报错内容： django.db.utils.InternalError: (1366, “Incorrect string value: ‘\xE5\xAE\x9A\xE6\x97\xB6…’ for column ‘name’ at row 1”)<br>解决方法: <a href="https://blog.csdn.net/tzh476/article/details/52644271" target="_blank" rel="noopener">https://blog.csdn.net/tzh476/article/details/52644271</a><br>删除之前的库，创建一个新的数据库，使用utf8mb64   之前默认创建的都是latin1<br>不一样要删除之前的库，修改数据库的属性即可</p>
<h2 id="mysql-长度的问题"><a href="#mysql-长度的问题" class="headerlink" title="mysql 长度的问题"></a>mysql 长度的问题</h2><p>django.db.utils.InternalError: (1071, ‘Specified key was too long; max key length is 767 bytes’)<br><a href="https://blog.csdn.net/ljfphp/article/details/80406907" target="_blank" rel="noopener">https://blog.csdn.net/ljfphp/article/details/80406907</a><br><a href="https://www.orcode.com/question/407126_k280b8.html" target="_blank" rel="noopener">https://www.orcode.com/question/407126_k280b8.html</a></p>
<p>解决方法1: 升级Mysql5.6–&gt;&gt;Mysql5.7<br>centos7 下  对mysql的操作 <a href="https://blog.csdn.net/xufengzhu/article/details/81110982" target="_blank" rel="noopener">https://blog.csdn.net/xufengzhu/article/details/81110982</a><br><a href="https://blog.51cto.com/lisea/1941616" target="_blank" rel="noopener">https://blog.51cto.com/lisea/1941616</a></p>
<p>解决方法2 : 先使用utf8格式</p>
<h2 id="django-migrate的问题"><a href="#django-migrate的问题" class="headerlink" title="django migrate的问题"></a>django migrate的问题</h2><p>出现下面这个问题，基本上就是你的model设置错误了。有些字段类型需要固定的属性<br>如integerField字段需要default这个选项，添加完之后就Ok了</p>
<pre><code class="bash">Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit, and let me add a default in models.p </code></pre>

        </div>
        
        
        
        
    </div>
</div>










    
<div class="card card-transparent">
    <nav class="pagination is-centered" role="navigation" aria-label="pagination">
        <div class="pagination-previous">
            <a class="is-flex-grow has-text-black-ter" href="/">Previous</a>
        </div>
        <div class="pagination-next">
            <a class="is-flex-grow has-text-black-ter" href="/page/3/">Next</a>
        </div>
        <ul class="pagination-list is-hidden-mobile">
            
            <li><a class="pagination-link has-text-black-ter" href="/">1</a></li>
            
            <li><a class="pagination-link is-current" href="/page/2/">2</a></li>
            
            <li><a class="pagination-link has-text-black-ter" href="/page/3/">3</a></li>
            
            <li><a class="pagination-link has-text-black-ter" href="/page/4/">4</a></li>
            
        </ul>
    </nav>
</div>
</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level" style="margin-bottom:1rem">
            <div class="level-item has-text-centered">
                <div>
                    
                        <img class="image is-96x96 has-mb-6" src="https://www.gravatar.com/avatar/e0f4032c0f2d1068ffffbaf93c0bef52?s=96" alt="Xinyu Liu">
                    
                    
                    <p class="is-size-4 is-block">
                        Xinyu Liu
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        Alpha Lxy
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>Beijing, China</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level menu-list is-mobile" style="margin-bottom:1rem">
            <div class="level-item has-text-centered is-marginless">
                <a href="/archives/">
                    <p class="heading">
                        Posts
                    </p>
                    <p class="title has-text-weight-normal">
                        40
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/categories/">
                    <p class="heading">
                        Categories
                    </p>
                    <p class="title has-text-weight-normal">
                        13
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/tags/">
                    <p class="heading">
                        Tags
                    </p>
                    <p class="title has-text-weight-normal">
                        0
                    </p>
                </a>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://www.github.com/AlphaLxy" target="_blank">
                <i class="fab fa-github"></i>&nbsp;&nbsp;Follow</a>
        </div>
        
        
    </div>
</div>

    
        

    
        


    
        

    
        <div class="card widget is-hidden-mobile">
    <div class="card-content">
        <div class="menu">
        <h3 class="menu-label">
            Archives
        </h3>
        <ul class="menu-list">
        
        <li>
            <a class="level is-marginless" href="/archives/2020/01/">
                <span class="level-start">
                    <span class="level-item">January 2020</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">8</span>
                </span>
            </a>
        </li>
        
        <li>
            <a class="level is-marginless" href="/archives/2019/12/">
                <span class="level-start">
                    <span class="level-item">December 2019</span>
                </span>
                <span class="level-end">
                    <span class="level-item tag">32</span>
                </span>
            </a>
        </li>
        
        </ul>
        </div>
    </div>
</div>

    
    
        <div class="column-right-shadow is-hidden-widescreen ">
        
            
<div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            Recent
        </h3>
        
        <article class="media">
            
            <a href="/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="keepalive+lvs 四层负载均衡">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-05T05:06:09.000Z">2020-01-05</time></div>
                    <a href="/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" class="has-link-black-ter is-size-6">keepalive+lvs 四层负载均衡</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/iptables%E3%80%81firewall%E9%98%B2%E7%81%AB%E5%A2%99/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="iptables、firewall 防火墙">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-05T04:03:16.000Z">2020-01-05</time></div>
                    <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/iptables%E3%80%81firewall%E9%98%B2%E7%81%AB%E5%A2%99/" class="has-link-black-ter is-size-6">iptables、firewall 防火墙</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/netstat/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="netstat">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-05T03:51:48.000Z">2020-01-05</time></div>
                    <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/netstat/" class="has-link-black-ter is-size-6">netstat</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/04/blog_back_new/%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E9%83%A8%E7%BD%B2%E9%9C%80%E6%B1%82%E5%88%86%E6%9E%90/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="网络部署需求分析">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-04T02:22:09.000Z">2020-01-04</time></div>
                    <a href="/2020/01/04/blog_back_new/%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E9%83%A8%E7%BD%B2%E9%9C%80%E6%B1%82%E5%88%86%E6%9E%90/" class="has-link-black-ter is-size-6">网络部署需求分析</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/%E7%BD%91%E7%BB%9C/">网络</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/03/blog_back_new/linux/httpd%E6%9C%8D%E5%8A%A1/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="httpd服务">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-03T09:18:50.000Z">2020-01-03</time></div>
                    <a href="/2020/01/03/blog_back_new/linux/httpd%E6%9C%8D%E5%8A%A1/" class="has-link-black-ter is-size-6">httpd服务</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
    </div>
</div>

        
            
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                Categories
            </h3>
            <ul class="menu-list">
            <li>
        <a class="level is-marginless" href="/categories/blog-back-new/">
            <span class="level-start">
                <span class="level-item">blog_back_new</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">39</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/categories/blog-back-new/C%E8%AF%AD%E8%A8%80/">
            <span class="level-start">
                <span class="level-item">C语言</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/Python/">
            <span class="level-start">
                <span class="level-item">Python</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/Rust/">
            <span class="level-start">
                <span class="level-item">Rust</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/ansible/">
            <span class="level-start">
                <span class="level-item">ansible</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/django/">
            <span class="level-start">
                <span class="level-item">django</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/git/">
            <span class="level-start">
                <span class="level-item">git</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/linux/">
            <span class="level-start">
                <span class="level-item">linux</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/mysql/">
            <span class="level-start">
                <span class="level-item">mysql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E6%89%A9%E5%B1%95/">
            <span class="level-start">
                <span class="level-item">扩展</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
            <span class="level-start">
                <span class="level-item">数据结构</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E7%BD%91%E7%BB%9C/">
            <span class="level-start">
                <span class="level-item">网络</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E8%AE%BE%E5%A4%87/">
            <span class="level-start">
                <span class="level-item">设备</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li></ul></li>
            </ul>
        </div>
    </div>
</div>
        
        </div>
    
</div>

                




<div class="column is-4-tablet is-4-desktop is-3-widescreen is-hidden-touch is-hidden-desktop-only has-order-3 column-right ">
    
        
<div class="card widget">
    <div class="card-content">
        <h3 class="menu-label">
            Recent
        </h3>
        
        <article class="media">
            
            <a href="/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="keepalive+lvs 四层负载均衡">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-05T05:06:09.000Z">2020-01-05</time></div>
                    <a href="/2020/01/05/blog_back_new/linux/keepalive+lvs%E5%9B%9B%E5%B1%82%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" class="has-link-black-ter is-size-6">keepalive+lvs 四层负载均衡</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/iptables%E3%80%81firewall%E9%98%B2%E7%81%AB%E5%A2%99/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="iptables、firewall 防火墙">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-05T04:03:16.000Z">2020-01-05</time></div>
                    <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/iptables%E3%80%81firewall%E9%98%B2%E7%81%AB%E5%A2%99/" class="has-link-black-ter is-size-6">iptables、firewall 防火墙</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/netstat/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="netstat">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-05T03:51:48.000Z">2020-01-05</time></div>
                    <a href="/2020/01/05/blog_back_new/linux/linux%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86/netstat/" class="has-link-black-ter is-size-6">netstat</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/04/blog_back_new/%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E9%83%A8%E7%BD%B2%E9%9C%80%E6%B1%82%E5%88%86%E6%9E%90/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="网络部署需求分析">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-04T02:22:09.000Z">2020-01-04</time></div>
                    <a href="/2020/01/04/blog_back_new/%E7%BD%91%E7%BB%9C/%E7%BD%91%E7%BB%9C%E9%83%A8%E7%BD%B2%E9%9C%80%E6%B1%82%E5%88%86%E6%9E%90/" class="has-link-black-ter is-size-6">网络部署需求分析</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/%E7%BD%91%E7%BB%9C/">网络</a>
                    </p>
                </div>
            </div>
        </article>
        
        <article class="media">
            
            <a href="/2020/01/03/blog_back_new/linux/httpd%E6%9C%8D%E5%8A%A1/" class="media-left">
                <p class="image is-64x64">
                    <img class="thumbnail" src="/images/thumbnail.svg" alt="httpd服务">
                </p>
            </a>
            
            <div class="media-content">
                <div class="content">
                    <div><time class="has-text-grey is-size-7 is-uppercase" datetime="2020-01-03T09:18:50.000Z">2020-01-03</time></div>
                    <a href="/2020/01/03/blog_back_new/linux/httpd%E6%9C%8D%E5%8A%A1/" class="has-link-black-ter is-size-6">httpd服务</a>
                    <p class="is-size-7 is-uppercase">
                        <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a> / <a class="has-link-grey -link" href="/categories/blog-back-new/linux/">linux</a>
                    </p>
                </div>
            </div>
        </article>
        
    </div>
</div>

    
        
<div class="card widget">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                Categories
            </h3>
            <ul class="menu-list">
            <li>
        <a class="level is-marginless" href="/categories/blog-back-new/">
            <span class="level-start">
                <span class="level-item">blog_back_new</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">39</span>
            </span>
        </a><ul><li>
        <a class="level is-marginless" href="/categories/blog-back-new/C%E8%AF%AD%E8%A8%80/">
            <span class="level-start">
                <span class="level-item">C语言</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/Python/">
            <span class="level-start">
                <span class="level-item">Python</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/Rust/">
            <span class="level-start">
                <span class="level-item">Rust</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">5</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/ansible/">
            <span class="level-start">
                <span class="level-item">ansible</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">2</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/django/">
            <span class="level-start">
                <span class="level-item">django</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">8</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/git/">
            <span class="level-start">
                <span class="level-item">git</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/linux/">
            <span class="level-start">
                <span class="level-item">linux</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">4</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/mysql/">
            <span class="level-start">
                <span class="level-item">mysql</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E6%89%A9%E5%B1%95/">
            <span class="level-start">
                <span class="level-item">扩展</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">
            <span class="level-start">
                <span class="level-item">数据结构</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">1</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E7%BD%91%E7%BB%9C/">
            <span class="level-start">
                <span class="level-item">网络</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">3</span>
            </span>
        </a></li><li>
        <a class="level is-marginless" href="/categories/blog-back-new/%E8%AE%BE%E5%A4%87/">
            <span class="level-start">
                <span class="level-item">设备</span>
            </span>
            <span class="level-end">
                <span class="level-item tag">6</span>
            </span>
        </a></li></ul></li>
            </ul>
        </div>
    </div>
</div>
    
    
</div>

            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/">
                
                    <img src="/images/logo.svg" alt="noback" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 Alpaca&nbsp;
                Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> & <a
                        href="http://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                Visited by <span id="busuanzi_value_site_uv">0</span> users
                </span>
                
                </p>
            </div>
            <div class="level-end">
            
                <div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle">
                
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
                        
                        <i class="fab fa-creative-commons"></i>&nbsp;<i class="fab fa-creative-commons-by"></i>&nbsp;<i class="fab fa-creative-commons-nc"></i>&nbsp;<i class="fab fa-creative-commons-sa"></i>&nbsp;
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>&nbsp;
                        
                    </a>
                </p>
                
                </div>
            
            </div>
        </div>
    </div>
</footer>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("en");</script>


    
    
    
    <script src="/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/" target="_blank" rel="noopener">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="Back to Top" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    
    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>
    <script src="/js/clipboard.js" defer></script>
    

    
    
    

    


<script src="/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="Type something..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js" defer></script>
<link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/insight.css">
    
</body>
</html>